## Replication for Appendix Table 1
# In case of any questions, please email anselm.rink@gmail.com

rm(list=ls())
library(foreign)
library(fBasics)
library(psych)
setwd("")
data <- read.dta("")

## Index

#Economic marginalization index -- four variables: q7_has_job q8_has_income q8_income_last_month_ln q9_chance_become_rich
data$q8_income_last_month_ln <- ifelse(data$q8_income_last_month>0,log(data$q8_income_last_month),0)
(economically_marginalized_index <- alpha(data[, c("q7_has_job", "q8_has_income", "q8_income_last_month_ln", "q9_chance_become_rich")]))
economically_marginalized_index <- economically_marginalized_index$scores
economically_marginalized_index <- max(economically_marginalized_index, na.rm=T)-economically_marginalized_index
summary(economically_marginalized_index)

#Political marginalization index -- three variables q10_plans_to_vote q11_govt_represents_interest q12_feels_on_loosing_side
(politically_marginalized_index <- alpha(data[, c("q10_plans_to_vote", "q11_govt_represents_interest", "q12_feels_on_loosing_side")]))
politically_marginalized_index <- politically_marginalized_index$scores
politically_marginalized_index <- max(politically_marginalized_index, na.rm=T)-politically_marginalized_index
summary(politically_marginalized_index)

#Troubled family relations index -- three variables: q19_rlts_mother q20_rlts_father q21_respect_friends_family
(troubled_family_index <- alpha(data[,c("q19_rlts_mother", "q20_rlts_father", "q21_respect_friends_family" )]))
troubled_family_index <- troubled_family_index$scores
summary(troubled_family_index)

#Exposure to violence -- two variables: q15_witnesses_interrel_violence q16_witnesses_viol_musl_govt
(violence_exposure_index <- alpha(data[,c("q15_witnesses_interrel_violence", "q16_witnesses_viol_musl_govt")]))
violence_exposure_index <- violence_exposure_index$scores
summary(violence_exposure_index)

#Religiosity -- two variables: q22_religion q23_frequents_house_worship
(religiosity_index <- alpha(data[,c("q22_religion", "q23_frequents_house_worship")]))
religiosity_index <- religiosity_index$scores
summary(religiosity_index)

#Destructive life events -- five variables:  q17_friend_family_died q18_lost_job q18_arrested q18_house_of_worship_raided q18_stopped_speaking_parents q18_love_problems q18_friend_left_country
(destructive_events_index <- alpha(data[,c("q17_friend_family_died", "q18_lost_job", "q18_arrested", "q18_house_of_worship_raided", "q18_stopped_speaking_parents", "q18_love_problems", "q18_friend_left_country")]))
destructive_events_index <- destructive_events_index$scores
summary(destructive_events_index)

data$economically_marginalized_index <- economically_marginalized_index
data$politically_marginalized_index <- politically_marginalized_index
data$religiosity_index <- religiosity_index
data$destructive_events_index <- destructive_events_index
data$troubled_family_index <- troubled_family_index
data$violence_exposure_index <- violence_exposure_index

## Descriptive Stats

christian_d <- data[which(data$respondent_muslim==0),]
muslim_d <- data[which(data$respondent_muslim==1), ]

df_sample <- as.data.frame(cbind(data$q7_has_job, data$q8_income_last_month_ln,  data$q8_has_income, data$q9_chance_become_rich, data$economically_marginalized_index, data$q10_plans_to_vote, data$q11_govt_represents_interest, data$q12_feels_on_loosing_side, data$politically_marginalized_index, data$q19_rlts_mother, data$q20_rlts_father, data$q21_respect_friends_family, data$troubled_family_index, data$q15_witnesses_interrel_violence, data$q16_witnesses_viol_musl_govt, data$violence_exposure_index, data$q23_frequents_house_worship, data$q22_religion, data$religiosity_index, data$q17_friend_family_died, data$q18_lost_job, data$q18_arrested, data$q18_house_of_worship_raided, data$q18_stopped_speaking_parents, data$q18_love_problems, data$q18_friend_left_country, data$destructive_events_index, data$q1_age, data$q6_married, data$q13_b_knows_emigrant_som_eri, data$q5_convert, data$q3_kamba, data$q3_kikuyu, data$q3_luo, data$q3_somali, data$q3_other, data$respondent_muslim, data$respondent_male, data$q8_income_last_month))
colnames(df_sample) <- c(             "Employed",            "Income (ln)",    "Any Income",               "Economic Prospects",     "Economic Marginalization Index",              "Turnout",                  "Representation",          "Political Prospects",   "Political Marginalization Index","Maternal Relationship","Paternal Relationship",                   "Respect",    "Troubled Family Index",           "Inter-Religious Violence",           "State-Mulims Violence",    "Violence Exposure Index",                "Attendance",  "Identification",    "Religiosity Index",             "Lost relative",        "Lost Job",          "Arrest",                           "Raid",                          "Anomie",         "Lost Partner",           "Friend Emigrated", "Destructive Events Index",               "Age",       "Married",            "Knows Somali Migrant",       "Convert",       "Kamba",       "Kikuyu",       "Luo",       "Somali",        "Other", "Muslim", "Male", "Income (KES)")

df_muslim <- as.data.frame(cbind(muslim_d$q7_has_job, muslim_d$q8_income_last_month_ln, muslim_d$q8_has_income, muslim_d$q9_chance_become_rich, muslim_d$economically_marginalized_index, muslim_d$q10_plans_to_vote, muslim_d$q11_govt_represents_interest, muslim_d$q12_feels_on_loosing_side, muslim_d$politically_marginalized_index, muslim_d$q19_rlts_mother, muslim_d$q20_rlts_father, muslim_d$q21_respect_friends_family, muslim_d$troubled_family_index, muslim_d$q15_witnesses_interrel_violence, muslim_d$q16_witnesses_viol_musl_govt, muslim_d$violence_exposure_index, muslim_d$q23_frequents_house_worship, muslim_d$q22_religion, muslim_d$religiosity_index, muslim_d$q17_friend_family_died, muslim_d$q18_lost_job, muslim_d$q18_arrested, muslim_d$q18_house_of_worship_raided, muslim_d$q18_stopped_speaking_parents, muslim_d$q18_love_problems, muslim_d$q18_friend_left_country, muslim_d$destructive_events_index,muslim_d$q1_age, muslim_d$q6_married, muslim_d$q13_b_knows_emigrant_som_eri, muslim_d$q5_convert, muslim_d$q3_kamba, muslim_d$q3_kikuyu, muslim_d$q3_luo, muslim_d$q3_somali, muslim_d$q3_other, muslim_d$respondent_muslim, muslim_d$respondent_male, muslim_d$q8_income_last_month))
colnames(df_muslim) <- c(             "Employed",            "Income (ln)",    "Any Income",               "Economic Prospects",     "Economic Marginalization Index",              "Turnout",                  "Representation",          "Political Prospects",   "Political Marginalization Index","Maternal Relationship","Paternal Relationship",                   "Respect",    "Troubled Family Index",           "Inter-Religious Violence",           "State-Mulims Violence",    "Violence Exposure Index",                "Attendance",  "Identification",    "Religiosity Index",             "Lost relative",        "Lost Job",          "Arrest",                           "Raid",                          "Anomie",         "Lost Partner",           "Friend Emigrated", "Destructive Events Index",               "Age",       "Married",            "Knows Somali Migrant",       "Convert",       "Kamba",       "Kikuyu",       "Luo",       "Somali",        "Other", "Muslim", "Male", "Income (KES)")

df_christian <- as.data.frame(cbind(christian_d$q7_has_job, christian_d$q8_income_last_month_ln, christian_d$q8_has_income, christian_d$q9_chance_become_rich, christian_d$economically_marginalized_index, christian_d$q10_plans_to_vote, christian_d$q11_govt_represents_interest, christian_d$q12_feels_on_loosing_side, christian_d$politically_marginalized_index, christian_d$q19_rlts_mother, christian_d$q20_rlts_father, christian_d$q21_respect_friends_family, christian_d$troubled_family_index, christian_d$q15_witnesses_interrel_violence, christian_d$q16_witnesses_viol_musl_govt, christian_d$violence_exposure_index, christian_d$q23_frequents_house_worship, christian_d$q22_religion, christian_d$religiosity_index, christian_d$q17_friend_family_died, christian_d$q18_lost_job, christian_d$q18_arrested, christian_d$q18_house_of_worship_raided, christian_d$q18_stopped_speaking_parents, christian_d$q18_love_problems, christian_d$q18_friend_left_country, christian_d$destructive_events_index, christian_d$q1_age, christian_d$q6_married, christian_d$q13_b_knows_emigrant_som_eri, christian_d$q5_convert, christian_d$q3_kamba, christian_d$q3_kikuyu, christian_d$q3_luo, christian_d$q3_somali, christian_d$q3_other, christian_d$respondent_muslim, christian_d$respondent_male, christian_d$q8_income_last_month))
colnames(df_christian) <- c(             "Employed",            "Income (ln)",    "Any Income",               "Economic Prospects",     "Economic Marginalization Index",              "Turnout",                  "Representation",          "Political Prospects",   "Political Marginalization Index","Maternal Relationship","Paternal Relationship",                   "Respect",    "Troubled Family Index",           "Inter-Religious Violence",           "State-Mulims Violence",    "Violence Exposure Index",                "Attendance",  "Identification",    "Religiosity Index",             "Lost relative",        "Lost Job",          "Arrest",                           "Raid",                          "Anomie",         "Lost Partner",           "Friend Emigrated", "Destructive Events Index",               "Age",       "Married",            "Knows Somali Migrant",       "Convert",       "Kamba",       "Kikuyu",       "Luo",       "Somali",        "Other", "Muslim", "Male", "Income (KES)")

require(fBasics)
a1 <- basicStats(df_sample)
a2 <- basicStats(df_muslim)
a3 <- basicStats(df_christian)

summarytable <- rbind(a1[3,1:length(a1)],a1[4,1:length(a1)], a1[1,1:length(a1)]-a1[2,1:length(a1)], a1[7,1:length(a1)], a1[14,1:length(a1)], a2[1,1:length(a1)]-a2[2,1:length(a1)],a2[7,1:length(a1)], a2[14,1:length(a1)], a3[1,1:length(a1)]-a3[2,1:length(a1)], a3[7,1:length(a1)], a3[14,1:length(a1)])
summarytable <- as.matrix(t(summarytable))
summarytable
